//
//  IcMaterialsChartViewController.m
//  IceClear
//
//  Created by P & C on 4/6/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "IcMaterialsChartViewController.h"
#import "IcMaterial.h"


@implementation IcMaterialsChartViewController


@synthesize appModelsManager;


// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	// Use a web and Google Charts Api to construct a chart: http://code.google.com/apis/chart/index.html
	
	NSMutableArray* vals = [NSMutableArray array]; // first data series
	NSMutableArray* vals2 = [NSMutableArray array]; // second data series
	NSMutableArray* labels = [NSMutableArray array]; // axis labels
	NSArray* materials = [self.appModelsManager.materialsManager allMaterialsGrouped];
	NSArray* dryMats = [materials objectAtIndex:0];
	NSArray* liquidMats = [materials objectAtIndex:1];
	NSArray* blendMats = [materials objectAtIndex:2];
	NSMutableString* strBuilder = [NSMutableString string];
	
	// gradually construct the google charts api url
	
	[strBuilder appendString:@"https://chart.googleapis.com/chart?chs=625x400&cht=bvg"];
	[strBuilder appendString:@"&chtt=PET+Of+Various+Chemicals&chts=660000,12"];
	[strBuilder appendString:@"&chf=bg,lg,0,808080,0,EFEFEF,1|c,lg,0,E5ECF9,0,C3D9FF,1"]; // chart bg colors
	[strBuilder appendString:@"&chco=A2C180,3D7930"]; // color of different data series
	[strBuilder appendString:@"&chbh=a,0,23"]; // space occupied by each bar chart
	[strBuilder appendString:@"&chxt=y,t,r,t&chxr=0,-50,0|2,0,50"]; // kinds of axis and their range
	[strBuilder appendString:@"&chxs=0,AA0033,11,0,l,676767|1,224499,9,0,l,676767|2,AA0033,11,0,l,676767|3,224499,9,0,l,676767"]; // styling of axis
	[strBuilder appendString:@"&chg=0,20,0,0"]; // chart grids
	
	// use the name of the Material models as labels to top axis of the chart
	for (IcMaterial * m in dryMats) {
		[vals addObject:[NSString stringWithFormat:@"%d", m.lowestPracticalTemperature]];
		[labels addObject:m.name];
		if ([m.name isEqualToString:@"nacl"]) {
			[vals2 addObject:[NSString stringWithFormat:@"%d", 
							  [(IcMaterial *)[liquidMats objectAtIndex:0] lowestPracticalTemperature]]];
		}
		else if ([m.name isEqualToString:@"cacl"]) {
			[vals2 addObject:[NSString stringWithFormat:@"%d", 
							  [(IcMaterial *)[liquidMats objectAtIndex:1] lowestPracticalTemperature]]];
		}
		else if ([m.name isEqualToString:@"mgcl"]) {
			[vals2 addObject:[NSString stringWithFormat:@"%d", 
							  [(IcMaterial *)[liquidMats objectAtIndex:2] lowestPracticalTemperature]]];
		}
		else {
			[vals2 addObject:[NSString stringWithFormat:@"%d", 0]];
		}
	}
	for (IcMaterial * m in blendMats) {
		[vals addObject:[NSString stringWithFormat:@"%d", m.lowestPracticalTemperature]];
		[labels addObject:m.name];
	}
	// build the chart data portion of the url
	[strBuilder appendFormat:@"&chd=t:%@|%@", [vals componentsJoinedByString:@","], [vals2 componentsJoinedByString:@","]];
	[strBuilder appendString:@"&chds=-50,0"];
	[strBuilder appendString:@"&chdl=Dry|Liquid"];
	// build the chart axis labels portion of the url
	[strBuilder appendFormat:@"&chxl=1:|%@|2:|Pavement|Temp+(C)|3:|Chemicals", [labels componentsJoinedByString:@"|"]];
	[strBuilder appendString:@"&chxp=2,26,24|3,50"];
	
	// finalize the google charts api url
	NSString* googleChartsUrl = [NSString stringWithFormat:@"%@", strBuilder];
	
	NSLog(@"Google charts url: %@", googleChartsUrl);
	
	// put it in a html string
	NSString* htmlFormat = @"<html><head><title>None</title></head><body><center><img src=\"%@\" /></center></body>";
	NSString* html = [NSString stringWithFormat:htmlFormat, googleChartsUrl];
	
	// load the html string
	[mainWbView loadHTMLString:html baseURL:[NSURL URLWithString:@"http://www.google.com"]];
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return YES;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[mainWbView release];
	[appModelsManager release];
	[lblHeading release];
    [super dealloc];
}


@end
